#!/bin/bash
cd `dirname "$BASH_SOURCE"`
if
    test -f Caddyfile
then
    echo "Caddyfile exists. Skipping web server setup."
    read -p "Press any key to continue installation..."
    echo ""
    cd - >/dev/null
    return 2>/dev/null || exit -1  # return if script is source, exit if in process
fi
while [[ "$SYSREPTOR_WEBSERVER" != [yY] && "$SYSREPTOR_WEBSERVER" != [nN] ]]
do
    echo ""
    echo "SysReptor runs on localhost (127.0.0.1) by default."
    read -p "Should we setup a webserver (Caddy in Docker) for you to expose it to your local network or the Internet? [y/n]: " SYSREPTOR_WEBSERVER
    if [[ "$SYSREPTOR_WEBSERVER" == [yY] ]]
    then
        while [[ "$SYSREPTOR_LETSENCRYPT" != [yY] && "$SYSREPTOR_LETSENCRYPT" != [nN] ]]
        do
            echo ""
            echo "Should we take care of your webserver HTTPS certificate using LetsEncrypt?"
            echo "For this, you must set up:"
            echo " 1. a valid domain name resolving to your public IP address"
            echo " 2. port 80 of your must be publicly reachable"
            read -p "Want a LetsEncrypt webserver certificate? [y/n]: " SYSREPTOR_LETSENCRYPT
        done
        while ! case "$SYSREPTOR_CADDY_PORT" in ''|*[!0-9]*) false;;esac;
        do
            if [[ "$SYSREPTOR_LETSENCRYPT" == [yY] ]]
            then
                    default_port=443
            else
                    default_port=80
            fi
            read -p "What port should the webserver use? [$default_port] " SYSREPTOR_CADDY_PORT
            SYSREPTOR_CADDY_PORT=${SYSREPTOR_CADDY_PORT:-$default_port}
            if [[ "$SYSREPTOR_CADDY_PORT" -lt 1 || "$SYSREPTOR_CADDY_PORT" -gt 65535 ]]
            then
                echo "Invalid port number. Please enter a valid port number between 1 and 65535."
                SYSREPTOR_CADDY_PORT=""
            elif [[ "$SYSREPTOR_CADDY_PORT" -eq 8000 ]]
            then
                echo "The Django app uses port 8000 on 127.0.0.1. Please use a different port."
                SYSREPTOR_CADDY_PORT=""
            fi
        done
        if [[ "$SYSREPTOR_LETSENCRYPT" == [yY] ]]
        then
            while [ -z "$SYSREPTOR_CADDY_FQDN" ]
            do
                    read -p "What is your publicly reachable fully qualified domain name (e.g., sysreptor.example.com)? " SYSREPTOR_CADDY_FQDN
            done
        fi
    fi
done
if [[ "$SYSREPTOR_WEBSERVER" == [nN] ]]
then
    echo "Okay. Won't set up a webserver."
else
    # Create config
    echo """# Do not modify. This file is automatically generated.
# Changes will be overwritten.
$SYSREPTOR_CADDY_FQDN:$SYSREPTOR_CADDY_PORT

reverse_proxy http://127.0.0.1:8000""" > Caddyfile
    echo "Setting up your web server..."

    docker_compose_file="../docker-compose.yml"
    include_caddy="  - caddy/docker-compose.yml"
    if ! grep -q "^$include_caddy" "$docker_compose_file"
    then
        # Include Caddy in docker-compose.yml
        sed -i "s#include:#include:\n$include_caddy#" "$docker_compose_file"
    fi
fi
docker container stop sysreptor-caddy 1>/dev/null 2>&1 && docker container rm sysreptor-caddy 1>/dev/null 2>&1 || true
echo ""
cd - >/dev/null
return 2>/dev/null || true  # return if script is source

cd `dirname "$BASH_SOURCE"`
cd ..
docker compose up -d
cd - >/dev/null
